<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2019/2/19
 * Time: 15:30
 */

namespace app\admin\controller;

use think\Db;

class Platform extends Main
{
    public function _initialize()
    {
        parent::_initialize();
        $this->company_id = isession('company_id');
    }
    /******************************************** 超级管理员设置项 ***********************************************/
    /**
     * 平台列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if (isession('user_id') == 1) {
            $data = Db::name('platform')->order('odx asc')->paginate(12);
            $this->assign('list', $data);
            return $this->fetch('index');
        }
        $where['flag'] = 1;
        $where['type'] = ['neq', 1];
        $data = Db::name('platform')->where($where)->order('odx asc')->paginate(12);
        $this->assign('list', $data);
        return $this->fetch('indexlist');
    }

    /**
     * 新建平台
     * @return mixed
     */
    public function add()
    {
        if (post()) {
            $data = post();
            $data['type'] = $data['type'];
            $data['flag'] = $data['flag'];
            $data['odx'] = trim($data['odx']);
            $data['name'] = trim($data['name']);
            $data['english'] = trim($data['english']);
            $data['classify'] = trim($data['classify']);
            $data['timestamp'] = time();
            DB::name('platform')->insert($data);
            $this->success("添加成功");
        } else {
            return $this->fetch('add');
        }
    }

    /**
     * 更新编辑
     * @param $id
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit($id)
    {
        if (post()) {
            $data = post();
            $updateData['odx'] = trim($data['odx']);
            $updateData['type'] = $data['type'];
            $updateData['flag'] = $data['flag'];
            $updateData['name'] = trim($data['name']);
            $updateData['classify'] = trim($data['classify']);
            DB::name('platform')->where('id', $data['id'])->update($updateData);
            $this->success("更新成功");
        } else {
            $info = DB::name('platform')->where('id', $id)->find();
            $this->assign("info", $info);
            return $this->fetch('edit');
        }
    }

    /**
     * 删除
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        $data = post();
        if (checkArr($data['id'])) {
            $where['id'] = ['in', $data['id']];
            $pwhere['platform_id'] = ['in', $data['id']];
        } else {
            $where['id'] = $data['id'];
            $pwhere['platform_id'] = $data['id'];
        }
        Db::name('platform')->where($where)->delete();
        //删除平台公司自定义属性
        Db::name('platform_property')->where($pwhere)->delete();
        //删除公司属性对照信息
        Db::name('platform_contrast')->where($pwhere)->delete();
        $this->success("删除成功");
    }

    /**
     * 属性列表
     * @param $id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function shuxing()
    {
        $where['level'] = 1;
        $data = Db::name('platform_shuxing')->where($where)->order("odx asc")->paginate(15);
        $this->assign('list', $data);
        return $this->fetch('list');
    }

    /**
     * 添加属性名称
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function shuxingAdd()
    {
        $data = post();
        $insertData['name'] = $data['name'];
        $insertData['level'] = 1;
        $insertData['odx'] = 0;
        $insertData['pid'] = 0;
        $insertData['timestamp'] = time();
        Db::name('platform_shuxing')->insert($insertData);
        $this->success('添加成功');
    }

    /**
     * 编辑属性名称
     * @param $id
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function shuxingEdit($id)
    {
        if (post()) {
            $data['name'] = spost('name');
            $data['odx'] = ipost('odx');
            DB::name('platform_shuxing')->where('id', post('id'))->update($data);
            $this->success("更新成功");
        } else {
            $info = DB::name('platform_shuxing')->where('id', $id)->find();
            $this->assign("info", $info);
            return $this->fetch('shuxingEdit');
        }
    }

    /**
     * 删除属性名称
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function shuxingdel()
    {
        $data = post();
        if (checkArr($data['id'])) {
            $where = "id in (" . join(",", $data['id']) . ")";
            $whereOr = "pid in (" . join(",", $data['id']) . ")";
        } else {
            $where = "id =" . $data['id'];
            $whereOr = "pid =" . $data['id'];
        }
        Db::name('platform_shuxing')->where($where)->whereOr($whereOr)->delete();
        $this->success("删除成功");
    }

    /**
     * 属性详情
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function shuxingInfo($id)
    {
        $where['pid'] = $id;
        $where['level'] = 2;
        $data = Db::name('platform_shuxing')->where($where)->order("odx asc")->paginate(15);
        $this->assign('list', $data);
        return $this->fetch('shuxingInfo');
    }

    /**
     * 添加属性信息
     */
    public function shuxingInfoAdd()
    {
        $data = post();
        $insertData['name'] = $data['name'];
        $insertData['level'] = 2;
        $insertData['pid'] = $data['id'];
        $insertData['timestamp'] = time();
        Db::name('platform_shuxing')->insert($insertData);
        $this->success('添加成功');
    }

    /**
     * 编辑属性信息
     * @param $id
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function shuxingInfoEdit($id)
    {
        if (post()) {
            $data['name'] = spost('name');
            $data['odx'] = ipost('odx');
            DB::name('platform_shuxing')->where('id', post('id'))->update($data);
            $this->success("更新成功");
        } else {
            $info = DB::name('platform_shuxing')->where('id', $id)->find();
            $this->assign("info", $info);
            return $this->fetch('shuxingInfoEdit');
        }
    }

    /**
     * 删除属性信息
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function shuxingInfodel()
    {
        $data = post();
        if (checkArr($data['id'])) {
            $where = "id in (" . join(",", $data['id']) . ")";
        } else {
            $where = "id =" . $data['id'];
        }
        Db::name('platform_shuxing')->where($where)->delete();
        $this->success("删除成功");
    }

    /******************************************** 普通用户设置项 ***********************************************/

    /**
     * 添加自定义属性
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function propertyAdd()
    {
        if (post()) {
            $name = spost('name');
            $platform_id = ipost('platform_id');
            $uid = isession('user_id');
            //查找当前公司当前平台所有自定义属性
            $where['platform_id'] = $platform_id;
            $where['company_id'] = $this->company_id;
            $data = DB::name('platform_property')->field('name')->where($where)->select();
            checkArr($data) && $data = array_column($data, 'name');

            if (strpos($name, ',') !== false) {
                $insertArr = [];
                $names = explode(',', $name);
                foreach ($names as $value) {
                    if (checkString($value) && !in_array($value, $data)) {
                        $insertArr[] = array(
                            'name' => $value,
                            'platform_id' => $platform_id,
                            'uid' => $uid,
                            'timestamp' => time(),
                            'company_id' => $this->company_id
                        );
                    }
                }
                if (checkArr($insertArr)) {
                    Db::name('platform_property')->insertAll($insertArr);
                } else {
                    $this->error('添加属性已存在');
                }
            } elseif (checkString($name) && !in_array($name, $data)) {
                $insertData = array(
                    'name' => $name,
                    'platform_id' => $platform_id,
                    'uid' => $uid,
                    'timestamp' => time(),
                    'company_id' => $this->company_id
                );
                Db::name('platform_property')->insert($insertData);
            } else {
                $this->error('添加属性已存在');
            }
            $this->success('添加成功');
        } else {
            return $this->fetch('propertyAdd');
        }
    }

    /**
     * 属性详情
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function propertyInfo()
    {
        if (post()) {
            $name = spost('name');
            $platform_id = ipost('platform_id');
            $uid = isession('user_id');
            //清楚当前公司当前平台所有自定义属性
            $where['platform_id'] = $platform_id;
            $where['company_id'] = $this->company_id;
            DB::name('platform_property')->where($where)->delete();

            if (strpos($name, ',') !== false) {
                $insertArr = [];
                $names = explode(',', $name);
                foreach ($names as $value) {
                    if (checkString($value)) {
                        $insertArr[] = array(
                            'name' => $value,
                            'platform_id' => $platform_id,
                            'uid' => $uid,
                            'timestamp' => time(),
                            'company_id' => $this->company_id
                        );
                    }
                }
                Db::name('platform_property')->insertAll($insertArr);
            } elseif (checkString($name)) {
                $insertData = array(
                    'name' => $name,
                    'platform_id' => $platform_id,
                    'uid' => $uid,
                    'timestamp' => time(),
                    'company_id' => $this->company_id
                );
                Db::name('platform_property')->insert($insertData);
            }
            $this->success('修改成功');
        } else {
            //查找当前公司当前平台所有自定义属性
            $where['platform_id'] = iget('platform_id');
            $where['company_id'] = $this->company_id;
            $data = DB::name('platform_property')->field('name')->where($where)->select();
            if (checkArr($data)) {
                $data = array_column($data, 'name');
                $this->assign('info', join(',', $data));
            }
            return $this->fetch('propertyInfo');
        }
    }

    /**
     * 属性对照列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function contrast()
    {
        $platform_id = iget('platform_id');
        $where['platform_id'] = $platform_id;
        $where['company_id'] = $this->company_id;
        $data = Db::name('platform_contrast')
            ->where($where)
            ->paginate(15, false, ['query' =>
                [
                    'platform_id' => $platform_id,
                    'name' => sget('name')
                ]]);
        $this->assign('list', $data);
        return $this->fetch('contrast');
    }

    /**
     * 添加属性对照信息
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function contrastAdd()
    {
        if (post()) {
            $shuxing = strtolower(spost('shuxing'));
            $property = spost('property');
            $platform_id = ipost('platform_id');
            //检测产品属性是否存在
            $where['shuxing'] = $shuxing;
            $where['platform_id'] = $platform_id;
            $where['company_id'] = $this->company_id;
            $num = DB::name('platform_contrast')->where($where)->count();
            if ($num > 0) {
                $this->error('此产品属性已有对照属性信息');
            }

            $insertData = array(
                'shuxing' => $shuxing,
                'property' => $property,
                'platform_id' => $platform_id,
                'timestamp' => time(),
                'uid' => isession('user_id'),
                'company_id' => $this->company_id
            );
            DB::name('platform_contrast')->insert($insertData);
            $this->success('添加成功');
        } else {
            //查找当前公司当前平台所有自定义属性
            $where['platform_id'] = iget('platform_id');
            $where['company_id'] = $this->company_id;
            $data = DB::name('platform_property')->field('name')->where($where)->select();
            $this->assign('list', $data);
            return $this->fetch('contrastAdd');
        }
    }

    /**
     * 编辑关联属性
     * @param $id
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function contrastEdit($id)
    {
        if (post()) {
            $shuxing = strtolower(spost('shuxing'));
            $property = spost('property');
            $platform_id = ipost('platform_id');
            $id = ipost('id');
            //检测产品属性是否存在
            $where['id'] = ['neq', $id];
            $where['shuxing'] = $shuxing;
            $where['platform_id'] = $platform_id;
            $where['company_id'] = $this->company_id;
            $num = DB::name('platform_contrast')->where($where)->count();
            if ($num > 0) {
                $this->error('此产品属性已有对照属性信息');
            }

            $data['shuxing'] = $shuxing;
            $data['property'] = $property;
            DB::name('platform_contrast')->where('id', $id)->update($data);
            $this->success("更新成功");
        } else {
            $info = DB::name('platform_contrast')->where('id', $id)->find();
            $this->assign("info", $info);
            //查找当前公司当前平台所有自定义属性
            $where['platform_id'] = $info['platform_id'];
            $where['company_id'] = $this->company_id;
            $data = DB::name('platform_property')->field('name')->where($where)->select();
            $this->assign('list', $data);

            return $this->fetch('contrastEdit');
        }
    }

    /**
     * 删除属性对照信息
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function contrastDel()
    {
        $data = post();
        if (checkArr($data['id'])) {
            $where['id'] = ['in', $data['id']];
        } else {
            $where['id'] = $data['id'];
        }
        Db::name('platform_contrast')->where($where)->delete();
        $this->success("删除成功");
    }
}